package coreapi;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 演示如何自己实现一个 HttpServlet
 */
public class MyHttpServlet extends GenericServlet {

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        HttpServletResponse response = (HttpServletResponse) servletRequest;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
    }

    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getMethod();
        if (method.equalsIgnoreCase("GET"))
            doGet(request, response);
        if (method.equalsIgnoreCase("POST"))
            doPost(request, response);
    }

    private void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendError(405, "未定义doPost方法");
    }

    private void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendError(405, "未定义doGet方法");
    }


}
